package com.pengpai.pangucms.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pengpai.pangucms.entity.WebConfig;
import com.pengpai.pangucms.service.IWebConfigService;
import com.pengpai.pangucms.utils.R;
import com.pengpai.pangucms.utils.UserUtils;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author rxg
 * @since 2023-08-26
 */
@Controller
@RequestMapping("/webConfig")
public class WebConfigController {
    @Resource
    private IWebConfigService webConfigService;




    @GetMapping("/index")
    @PreAuthorize("hasAuthority('web:config:read')")
    public String index(){
        return "admin/web_config/web_config_index";
    }

    @GetMapping("/add")
    @PreAuthorize("hasAuthority('web:config:update')")
    public String add(){
        return "admin/web_config/web_config_add";
    }

    @GetMapping("edit")
    @PreAuthorize("hasAuthority('web:config:update')")
    public String edit(Model model, String id){
        WebConfig webConfig = webConfigService.getById(id);
        model.addAttribute("config",webConfig);
        return "admin/web_config/web_config_edit";
    }

    @GetMapping("/list")
    @ResponseBody
    @PreAuthorize("hasAuthority('web:config:read')")
    public Map<String,Object> list(String key, int page, int limit){

        Page<WebConfig> webConfigPage = new Page<>(page,limit);

        QueryWrapper<WebConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(key), "config_key", key).orderByAsc("config_code").orderByAsc("order_id");

        List<WebConfig> webConfigs = webConfigService.page(webConfigPage,queryWrapper).getRecords();

        Map<String, Object> map = new HashMap<>();

        map.put("code",0);
        map.put("msg","");
        map.put("count",webConfigPage.getTotal());
        map.put("data",webConfigs);
        map.put("key",key);

        return map;
    }

    @PostMapping("/create")
    @ResponseBody
    @PreAuthorize("hasAuthority('web:config:create')")
    public R create(WebConfig webConfig){

        webConfig.setCreateUser(UserUtils.CurrentUserName());
        webConfig.setCreateTime(LocalDateTime.now());
        webConfig.setUpdateUser(UserUtils.CurrentUserName());
        webConfig.setUpdateTime(LocalDateTime.now());

        webConfigService.save(webConfig);
        return R.success();
    }

    @PostMapping("/del")
    @ResponseBody
    @PreAuthorize("hasAuthority('web:config:delete')")
    public R del(String ids){

        List<Long> list = null;
        if(StrUtil.isNotBlank(ids)){
            list = Stream.of(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        }
        webConfigService.removeBatchByIds(list);
        return R.success();
    }
    @PostMapping("/update")
    @ResponseBody
    @PreAuthorize("hasAuthority('web:config:update')")
    public R update(WebConfig webConfig){


        webConfig.setCreateTime(LocalDateTime.now());
        webConfig.setUpdateUser(UserUtils.CurrentUserName());
        webConfig.setUpdateTime(LocalDateTime.now());
        webConfigService.updateById(webConfig);
        return R.success();
    }


}
